﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MaxLeaf.KH.DataContract;
using System.Transactions;

namespace WebProxyUnitTest
{
    [TestClass]
    public class IBasicMtnUnitTest
    {
         //在运行类中的第一个测试之前使用 ClassInitialize 运行代码
         [ClassInitialize()]
         public static void MyClassInitialize(TestContext testContext) {

             
         }
        
         //在类中的所有测试都已运行之后使用 ClassCleanup 运行代码
         [ClassCleanup()]
         public static void MyClassCleanup() {
             
         }

         [TestInitialize()]
         public void Initialize()
         {
             CommonsLib.DB.DBAccess dbcon = new CommonsLib.DB.DBAccess(new MaxLeaf.KH.DALImplementation.BaseDAL().GetConnectionInfo("DB_A"));
             dbcon.ExecuteSql("delete from a");
         }

        [TestMethod]
        public void TestMethod1()
        {
            
                    Method1();
                

        }

        [TestMethod]
        public void TestSelectDtl()
        {

            Dictionary<string, object> inputData = null;
            Dictionary<string, object> outputData = null;
            string cmd = string.Empty;

            MaxLeaf.KH.WebProxy.Proxy proxy = new MaxLeaf.KH.WebProxy.Proxy();

            PrepairSelectDtlData(proxy);

            List<object> users = null;
            //SelectDtl
            cmd = "SelectDtl";
            inputData = new Dictionary<string, object>();
            inputData["data"] = new TestModel();
            inputData["KeyValue"] = 1;            
            outputData = (Dictionary<string, object>)proxy.Execute(cmd, inputData);
            users = ((List<object>)outputData["data"]);
            Assert.AreEqual<int>(users.Count, 2);
            Assert.AreEqual<int>(((TestModel)users[0]).HdrID, 1);
            Assert.AreEqual<string>(((TestModel)users[0]).Name, "Zhang san");
            Assert.AreEqual<int>(((TestModel)users[1]).HdrID, 1);
            Assert.AreEqual<string>(((TestModel)users[1]).Name, "Zhao liu");


            cmd = "SelectDtl";
            inputData = new Dictionary<string, object>();
            inputData["data"] = new TestModel();
            inputData["KeyValue"] = 2;
            outputData = (Dictionary<string, object>)proxy.Execute(cmd, inputData);
            users = ((List<object>)outputData["data"]);
            Assert.AreEqual<int>(users.Count, 2);
            Assert.AreEqual<int>(((TestModel)users[0]).HdrID, 2);
            Assert.AreEqual<string>(((TestModel)users[0]).Name,"Li si");
            Assert.AreEqual<int>(((TestModel)users[1]).HdrID, 2);
            Assert.AreEqual<string>(((TestModel)users[1]).Name, "Wang wu");

            cmd = "SelectDtl";
            inputData = new Dictionary<string, object>();
            inputData["data"] = new TestModel();
            inputData["KeyValue"] = 3;
            outputData = (Dictionary<string, object>)proxy.Execute(cmd, inputData);
            users = ((List<object>)outputData["data"]);
            Assert.AreEqual<int>(users.Count, 0);
        }

        private void PrepairSelectDtlData(  MaxLeaf.KH.WebProxy.Proxy proxy)
        {

            Dictionary<string, object> inputData = null;
            Dictionary<string, object> outputData = null;
            string cmd = string.Empty;
            //Add
            TestModel user = new TestModel { HdrID = 1, Name = "Zhang san" };
            inputData = GetAddData(user);
            cmd = "Add";
            outputData = (Dictionary<string, object>)proxy.Execute(cmd, inputData);

            user = new TestModel { HdrID = 2, Name = "Li si" };
            inputData = GetAddData(user);
            cmd = "Add";
            outputData = (Dictionary<string, object>)proxy.Execute(cmd, inputData);

            user = new TestModel { HdrID = 2, Name = "Wang wu" };
            inputData = GetAddData(user);
            cmd = "Add";
            outputData = (Dictionary<string, object>)proxy.Execute(cmd, inputData);

            user = new TestModel { HdrID = 1, Name = "Zhao liu" };
            inputData = GetAddData(user);
            cmd = "Add";
            outputData = (Dictionary<string, object>)proxy.Execute(cmd, inputData);
        }

        private void Method1()
        {
            Dictionary<string, object> inputData = null;
            Dictionary<string, object> outputData = null;
            string cmd = string.Empty;

            string strName = "zhang hai";
            TestModel user = new TestModel();
            user.Name = strName;

            MaxLeaf.KH.WebProxy.Proxy proxy = new MaxLeaf.KH.WebProxy.Proxy();

            //Add
            inputData = GetAddData(user);
            cmd = "Add";
            outputData = (Dictionary<string, object>)proxy.Execute(cmd, inputData);
            user = ((TestModel)outputData["data"]);
            int id = user.ID;
            Assert.IsTrue(id > 0, "id must be greater than zero");

            //Select
            inputData = GetSelectData(id);
            cmd = "Select";
            outputData = (Dictionary<string, object>)proxy.Execute(cmd, inputData);
            user = ((TestModel)outputData["data"]);
            Assert.AreEqual<int>(id, user.ID, "id not same");
            Assert.AreEqual<string>(strName, user.Name, "name not same");

            //Update
            string strNewName = "zhou yong";
            user.Name = strNewName;
            inputData = GetUpdateData(user);
            cmd = "Update";
            outputData = (Dictionary<string, object>)proxy.Execute(cmd, inputData);
            user = ((TestModel)outputData["data"]);
            Assert.AreEqual<int>(id, user.ID, "id not same");
            Assert.AreEqual<string>(strNewName, user.Name, "name not same");

            inputData = GetSelectData(user.ID);
            cmd = "Select";
            outputData = (Dictionary<string, object>)proxy.Execute(cmd, inputData);
            user = ((TestModel)outputData["data"]);
            Assert.AreEqual<int>(id, user.ID, "id not same");
            Assert.AreEqual<string>(strNewName, user.Name, "name not same");

            //Delete
            id = user.ID;
            inputData = GetDeleteData(user);
            cmd = "Delete";
            outputData = (Dictionary<string, object>)proxy.Execute(cmd, inputData);
            user = ((TestModel)outputData["data"]);


            inputData = GetSelectData(user.ID);
            cmd = "Select";
            outputData = (Dictionary<string, object>)proxy.Execute(cmd, inputData);
            user = ((TestModel)outputData["data"]);
            Assert.IsNull(user, "user must be delete");
        }

        private Dictionary<string, object> GetDeleteData(TestModel user)
        {
            Dictionary<string, object> inputData = new Dictionary<string, object>();

            inputData["data"] = user;
            return inputData;
        }

        private Dictionary<string, object> GetUpdateData(TestModel user)
        {
            Dictionary<string, object> inputData = new Dictionary<string, object>();

            inputData["data"] = user;
            return inputData;
        }

        private Dictionary<string, object> GetSelectData(int id)
        {
            Dictionary<string, object> inputData = new Dictionary<string, object>();

            inputData["data"] = new TestModel();
            inputData["KeyValue"] = id;
            return inputData;
        }

        private Dictionary<string, object> GetAddData(TestModel user)
        {
            Dictionary<string, object> inputData = new Dictionary<string, object>();
            
            inputData["data"] = user;
            return inputData;
        }
    }
}
